package ControlLayer;

import ModelLayer.*;
import java.util.ArrayList;
/**
 * Controller class for the DVD collection
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * @revised version English (Ab)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class DVDCtr
{
    //declare instance variables to reference containers
    //passed as arguments from the main class
    
    private ExampleContainer exampleContainer;

    /**
     * Constructor for objects of class DVDCtr
     */
    public DVDCtr()
    {
        exampleContainer  = ExampleContainer.getInstance(); 
        
    }
    
    /**
     * Returns an ArrayList of examples, which dvd-object contains title
     * @param   title   a part (or whole) of the title of the sought dvd
     */
    public ArrayList<Example> findDVD(String title)
    {
        ArrayList<Example> dvdExamples = new ArrayList<Example>();
        
        //check all examples in exampleCotainer for a dvd-object with a part of the title beeing title (last = parameter)
        for(Example example : exampleContainer.getAllExamples())
        {
            //is this example allready lent out? if not, proceed
            if(example.isLentOut() == false)
            {
                //get the title from the dvd associated with this example
                String exampleDvdTitle = example.getDvd().getTitle();
                
                //if the titles are equal
                if(exampleDvdTitle.equals(title))
                {
                    //add this example to the 
                    dvdExamples.add(example);
                }
            }
        }
        
        return dvdExamples;
    }
    
    /**
     * Creates a DVD with one example in it
     */
    public void createDVD(int serialNumber, String dateOfPurchase, double priceOfPurchase, Dvd dvd)
    {
        exampleContainer.addExample(serialNumber, dateOfPurchase, priceOfPurchase, dvd);
    }
    
    /**
     * Removes one example of a DVD object, identified by the serialNumber of the example 
     */
    public void deleteDVD(int serialNumber)
    {
        exampleContainer.removeExample(serialNumber);
    }
    
    /**
     * Creates a new copy of an old dvdExample w/ a new serialNumber
     */
    public Example createCopy(int serialNumber)
    {
        for(Example example : exampleContainer.getAllExamples())
        {
            if(example.getSerialNumber() == serialNumber)
            {
                return example;
            }
        }
        
        return null;
    }
    
    /**
     * Returns all DVD examples
     */
    public ArrayList<Example> listAllDvds()
    {
        return exampleContainer.getAllExamples();
    }
}
